package com.turnengine.client.local.message.bean;

import com.turnengine.client.local.message.enums.LocalMessageStatus;
import com.turnengine.client.local.message.enums.LocalMessageType;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Local Message Header.
 */
public class LocalMessageHeader implements ILocalMessageHeader, Comparable<ILocalMessageHeader> {

	/** The id. */
	private int id = -1;
	/** The sender id. */
	private int senderId = -1;
	/** The recipient id. */
	private int recipientId = -1;
	/** The subject. */
	private String subject = "";
	/** The thread id. */
	private int threadId = -1;
	/** The timestamp. */
	private long timestamp = 0;
	/** The status. */
	private LocalMessageStatus status = LocalMessageStatus.UNREAD;
	/** The type. */
	private LocalMessageType type = LocalMessageType.MAIL;

	/**
	 * Creates a new Local Message Header.
	 */
	public LocalMessageHeader() {
	}

	/**
	 * Creates a new Local Message Header.
	 * @param id the id
	 * @param senderId the sender id
	 * @param recipientId the recipient id
	 * @param subject the subject
	 * @param threadId the thread id
	 * @param timestamp the timestamp
	 * @param status the status
	 * @param type the type
	 */
	public LocalMessageHeader(int id, int senderId, int recipientId, String subject, int threadId, long timestamp, LocalMessageStatus status, LocalMessageType type) {
		setId(id);
		setSenderId(senderId);
		setRecipientId(recipientId);
		setSubject(subject);
		setThreadId(threadId);
		setTimestamp(timestamp);
		setStatus(status);
		setType(type);
	}

	/**
	 * Creates a new Local Message Header.
	 * @param localMessageHeader the local message header
	 */
	public LocalMessageHeader(LocalMessageHeader localMessageHeader) {
		setId(localMessageHeader.getId());
		setSenderId(localMessageHeader.getSenderId());
		setRecipientId(localMessageHeader.getRecipientId());
		setSubject(localMessageHeader.getSubject());
		setThreadId(localMessageHeader.getThreadId());
		setTimestamp(localMessageHeader.getTimestamp());
		setStatus(localMessageHeader.getStatus());
		setType(localMessageHeader.getType());
	}

	/**
	 * Creates a new Local Message Header.
	 * @param iLocalMessageHeader the i local message header
	 */
	public LocalMessageHeader(ILocalMessageHeader iLocalMessageHeader) {
		setId(iLocalMessageHeader.getId());
		setSenderId(iLocalMessageHeader.getSenderId());
		setRecipientId(iLocalMessageHeader.getRecipientId());
		setSubject(iLocalMessageHeader.getSubject());
		setThreadId(iLocalMessageHeader.getThreadId());
		setTimestamp(iLocalMessageHeader.getTimestamp());
		setStatus(iLocalMessageHeader.getStatus());
		setType(iLocalMessageHeader.getType());
	}

	/**
	 * Returns the id.
	 * @return the id.
	 */
	public int getId() {
		return id;
	}

	/**
	 * Returns the sender id.
	 * @return the sender id.
	 */
	public int getSenderId() {
		return senderId;
	}

	/**
	 * Returns the recipient id.
	 * @return the recipient id.
	 */
	public int getRecipientId() {
		return recipientId;
	}

	/**
	 * Returns the subject.
	 * @return the subject.
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * Returns the thread id.
	 * @return the thread id.
	 */
	public int getThreadId() {
		return threadId;
	}

	/**
	 * Returns the timestamp.
	 * @return the timestamp.
	 */
	public long getTimestamp() {
		return timestamp;
	}

	/**
	 * Returns the status.
	 * @return the status.
	 */
	public LocalMessageStatus getStatus() {
		return status;
	}

	/**
	 * Returns the type.
	 * @return the type.
	 */
	public LocalMessageType getType() {
		return type;
	}

	/**
	 * Sets the id.
	 * @param id the id to set.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Sets the sender id.
	 * @param senderId the sender id to set.
	 */
	public void setSenderId(int senderId) {
		this.senderId = senderId;
	}

	/**
	 * Sets the recipient id.
	 * @param recipientId the recipient id to set.
	 */
	public void setRecipientId(int recipientId) {
		this.recipientId = recipientId;
	}

	/**
	 * Sets the subject.
	 * @param subject the subject to set.
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * Sets the thread id.
	 * @param threadId the thread id to set.
	 */
	public void setThreadId(int threadId) {
		this.threadId = threadId;
	}

	/**
	 * Sets the timestamp.
	 * @param timestamp the timestamp to set.
	 */
	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	/**
	 * Sets the status.
	 * @param status the status to set.
	 */
	public void setStatus(LocalMessageStatus status) {
		this.status = status;
	}

	/**
	 * Sets the type.
	 * @param type the type to set.
	 */
	public void setType(LocalMessageType type) {
		this.type = type;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(827, 37);
		hash.append(id);
		hash.append(senderId);
		hash.append(recipientId);
		hash.append(subject);
		hash.append(threadId);
		hash.append(timestamp);
		hash.append(status);
		hash.append(type);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof LocalMessageHeader) {
			LocalMessageHeader compare = (LocalMessageHeader) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.id, compare.id);
			equals.append(this.senderId, compare.senderId);
			equals.append(this.recipientId, compare.recipientId);
			equals.append(this.subject, compare.subject);
			equals.append(this.threadId, compare.threadId);
			equals.append(this.timestamp, compare.timestamp);
			equals.append(this.status, compare.status);
			equals.append(this.type, compare.type);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(ILocalMessageHeader compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.id, compare.getId());
		builder.append(this.senderId, compare.getSenderId());
		builder.append(this.recipientId, compare.getRecipientId());
		builder.append(this.subject, compare.getSubject());
		builder.append(this.threadId, compare.getThreadId());
		builder.append(this.timestamp, compare.getTimestamp());
		builder.append(this.status, compare.getStatus());
		builder.append(this.type, compare.getType());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("id", id);
		string.append("senderId", senderId);
		string.append("recipientId", recipientId);
		if (subject != null) {
			string.append("subject", subject);
		}
		string.append("threadId", threadId);
		string.append("timestamp", timestamp);
		if (status != null) {
			string.append("status", status);
		}
		if (type != null) {
			string.append("type", type);
		}
		return string.toString();
	}
}
